﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Xml.Serialization;

namespace ModbusTest.Config
{
    [Serializable]
    public class SpecificationModel
    {
        [DisplayName("编号"), Category("常规")]
        public string Name { get; set; }
        [DisplayName("描述"), Category("常规")]
        public string Description { get; set; }
        [DisplayName("站别"), Category("常规")]
        public string StationName { get; set; }
        [XmlIgnore,DisplayName("测试值"), Category("常规")]
        public float TestValue { get; set; }
        [DisplayName("测试结果"), Category("常规")]
        public bool TestResult { get; set; }
        [DisplayName("标准值"), Category("标准")]
        public float StandardValue { get; set; }
        [DisplayName("上偏差"), Category("标准")]
        public float UpperOffset { get; set; }
        [DisplayName("下偏差"), Category("标准")]
        public float LowerOffset { get; set; }
        [DisplayName("PLC编号")]
        public ushort PLCLocation { get; set; }
        [DisplayName("Modbus标准值地址"), Category("地址")]
        public ushort Addr_Standard { get; set; }
        [DisplayName("Modbus上限值地址"), Category("地址")]
        public ushort Addr_Upper { get; set; }
        [DisplayName("Modbus下限值地址"), Category("地址")]
        public ushort Addr_Lower { get; set; }
        [DisplayName("Modbus测试值地址"), Category("地址")]
        public ushort Addr_Value { get; set; }
        [DisplayName("Modbus测试结果地址"), Category("地址")]
        public ushort Addr_Result { get; set; }
    }
    public class TEST_NAME
    {
        public const string NAME1 = "11";
        public const string NAME2 = "12";
        public const string NAME3 = "13";
        public const string NAME4 = "21";
        public const string NAME5 = "22";
        public const string NAME6 = "23";
        public const string NAME7 = "31";
        public const string NAME8 = "32";
        public const string NAME9 = "33";
        public const string NAME10 = "41";
        public const string NAME11 = "42";
        public const string NAME12 = "43";
        public const string NAME13 = "51";
        public const string NAME14 = "52";
    }
    [Serializable]
    public class ModuleConfigContainer
    {
        public ModuleConfigContainer()
        {

        }
        List<ModuleConfig> _Modules = new List<ModuleConfig>();
        public List<ModuleConfig> Modules
        {
            get { return _Modules; }
            set { _Modules = value; }
        }
        public void Add(ModuleConfig cfg)
        {
            _Modules.Add(cfg);
        }
        public ModuleConfig Find(string Name)
        {
            var s = from a in _Modules where a.Name == Name select a;
            if (s.Count() > 0)
            {
                return s.ToArray()[0];
            }
            else
                return null;
        }
        public void Remove(string Name)
        {
            ModuleConfig cfg = Find(Name);
            _Modules.Remove(cfg);
        }
    }
    [Serializable]
    public class ModuleConfig
    {
        List<SpecificationModel> _specifications = new List<SpecificationModel>();
        [DisplayNameAttribute("参数设置")]
        public List<SpecificationModel> Specifications { get { return _specifications; } set { _specifications = value; } }
        [DisplayNameAttribute("机种名")]
        public string Name { get; set; }
        [DisplayNameAttribute("检测信号地址")]
        public ushort Signal_Check_Addr { get; set; }
        public ModuleConfig()
        {
        }
        public ModuleConfig(string ModuleName)
        {
            Name = ModuleName;
            Add(new SpecificationModel() { Name = TEST_NAME.NAME1 });
            Add(new SpecificationModel() { Name = TEST_NAME.NAME2 });
            Add(new SpecificationModel() { Name = TEST_NAME.NAME3 });
            Add(new SpecificationModel() { Name = TEST_NAME.NAME4 });
            Add(new SpecificationModel() { Name = TEST_NAME.NAME5 });
            Add(new SpecificationModel() { Name = TEST_NAME.NAME6 });
            Add(new SpecificationModel() { Name = TEST_NAME.NAME7 });
            Add(new SpecificationModel() { Name = TEST_NAME.NAME8 });
            Add(new SpecificationModel() { Name = TEST_NAME.NAME9 });
            Add(new SpecificationModel() { Name = TEST_NAME.NAME10 });
            Add(new SpecificationModel() { Name = TEST_NAME.NAME11 });
            Add(new SpecificationModel() { Name = TEST_NAME.NAME12 });
            Add(new SpecificationModel() { Name = TEST_NAME.NAME13 });
            Add(new SpecificationModel() { Name = TEST_NAME.NAME14 });
        }
        private SpecificationModel this[string Name]
        {
            get
            {
                return getModelbyName(Name);
            }
            set
            {
                SpecificationModel model = getModelbyName(Name);
                model = value;
            }
        }
        private SpecificationModel getModelbyName(string Name)
        {
            var s = from a in _specifications where a.Name == Name select a;
            return s.Count() > 0 ? s.ToArray()[0] : null;
        }
        private void Add(SpecificationModel model)
        {
            _specifications.Add(model);
        }

        private void Remove(string Name)
        {
            var s = from a in _specifications where a.Name == Name select a;
            foreach (var b in s)
            {
                _specifications.Remove(b);
            }
        }
    }
}
